<?php
/**
 * 视图控制器
 */
namespace app\manage\controller\order;
use app\manage\controller\Base;

class Api extends Base
{
    public function order(){
        $params = $this->request->param();
        $perpagenum = isset($params['limit'])?intval($params['limit']):15;
        $perpagenum = $perpagenum>100?100:$perpagenum;
        $page = isset($params['page'])?intval($params['page']):1;
        $page = $page<1?1:$page;

        unset($params['limit']);
        unset($params['page']);
        $where = [];
        if(isset($params['key'])){
            $where= parseWhere($params['key']);
        }
        $data = model('Order')->getPageDatas($where,$page,$perpagenum,'id,order_id,uid,amount,status,create_time,des,credit');
        $data['code'] = 1;
        return json($data);
    }

    public function details(){
        $params = $this->request->param();
        $data = model('Order')->getOneDatas(['order_id'=>$params['order_id']]);
        $data['code'] = 1;
        return json($data);
    }

    /**
     * 状态接口
     * @return [type] [description]
     */
    public function status_order(){
        $params = $this->request->param();
        $status = isset($params['status'])?$params['status']:null;
        $data = model('Order')->status($status);
        $data['code'] = 1;
        return json($data);
    }

    /**
     * 导出订单列表
     * @return [type] [description]
     */
    public function export_index(){
        $params = $this->request->param();
        $where = parseWhere($params);
        $datas = model('Order')->getAllRow($where,'order_id,uid,amount,status,des,create_time,credit,score');
        $excelData = "订单号\t订单用户\t订单总价格\t订单状态\t订单详细描述\t订单创建时间\t订单抵扣积分\t订单所得业绩\t\n";
        foreach($datas as $value):
            $excelData .= $value['order_id']."\t";
            $excelData .= $value['mid']."\t";
            $excelData .= $value['amount']."\t";
            $excelData .= $value['chs_status']."\t";
            $excelData .= $value['des']."\t";
            $excelData .= $value['create_time']."\t";
            $excelData .= $value['credit']."\t";
            $excelData .= $value['score']."\t";
            $excelData .= "\n";
        endforeach;
        $excelData = iconv('utf-8','gb2312',$excelData);
        exportExcel(time().'.xls',$excelData);
    }


    public function parseWhere($where){
        $array = [];
        foreach($where as $key => $value):
            if(!$value)
                continue;
            switch ($key) {
                case 'uid':
                    $array[$key] = ['like','%'.$value.'%'];
                    break;
                case 'status':
                    $array[$key] = $value;
                    break;
                case 'order_id':
                    $array[$key] = $value;
                    break;
                case 'start':
                    $array['create_time'] = ['>=',strtotime($value)];
                    break;
                case 'end':
                    $array['create_time'] = ['<=',strtotime($value)];
                    break;
                default:
                    $array[$key] = $value;
                    break;
            }
        endforeach;
        return $array;
    }
}